Minor changes to Translation Agent

Rishabh Jain 12 years ago
parent
commit
b755853c3d
1 changed files with 15 additions and 9 deletions
  1. 15 9
      app/models/agents/translation_agent.rb

+ 15 - 9
app/models/agents/translation_agent.rb

@@ -35,13 +35,13 @@ module Agents
35 35
         end
36 36
 
37 37
         def translate(text,to,access_token)
38
-            translate_uri = URI "http://api.microsofttranslator.com/v2/Ajax.svc/Translate"
38
+            translate_uri = URI 'http://api.microsofttranslator.com/v2/Ajax.svc/Translate'
39 39
             params = {
40 40
                 :text => text,
41 41
                 :to   => to
42 42
             }
43 43
             translate_uri.query = URI.encode_www_form params
44
-            request = Net::HTTP::Get.new translate_uri
44
+            request = Net::HTTP::Get.new translate_uri.request_uri
45 45
             request['Authorization'] = "Bearer" + " " + access_token
46 46
             http = Net::HTTP.new translate_uri.hostname, translate_uri.port 
47 47
             response = http.request request 
@@ -54,21 +54,27 @@ module Agents
54 54
             end
55 55
         end
56 56
 
57
+        def postform(uri,params)
58
+            req = Net::HTTP::Post.new(uri.request_uri)
59
+            req.form_data = params
60
+            Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) { |http| http.request(req) }
61
+        end
62
+
57 63
         def receive(incoming_events)
58 64
             auth_uri = URI "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"
59
-            response = Net::HTTP.post_form auth_uri, :client_id => options[:client_id], 
60
-                                                     :client_secret => options[:client_secret], 
61
-                                                     :scope => "http://api.microsofttranslator.com", 
62
-                                                     :grant_type =>  "client_credentials"
65
+            response = postform auth_uri, :client_id => options[:client_id],
66
+                                          :client_secret => options[:client_secret],
67
+                                          :scope => "http://api.microsofttranslator.com",
68
+                                          :grant_type =>"client_credentials"
63 69
             access_token = JSON.parse(response.body)["access_token"]
64 70
             incoming_events.each do |event|
65 71
                 translated_event = {}
66 72
                 options[:content].each_pair do |key,value|
67
-                    translate_value = Utils.values_at event.payload, value 
68
-                    translated_event[key] = translate translate_value.first, options[:to], access_token
73
+                    to_be_translated = Utils.values_at event.payload, value 
74
+                    translated_event[key] = translate to_be_translated.first, options[:to], access_token
69 75
                 end
70 76
                 create_event :payload => translated_event
71 77
             end
72 78
         end
73 79
     end
74
-end
80
+end